package Interface.Panel;

import Controller.InputController;
import Controller.PeasantController;
import Entity.Peasant;
import java.util.ArrayList;
import javax.persistence.EntityManagerFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Esteban
 */
public class PeasantsListPanel extends javax.swing.JPanel {

    private PeasantController peasantsController;
    private InputController validator;
    private String confirmDeleteMessage = "\"Desea eliminar este usuario";
    private String noElementSelectedMessage = "\"Por favor seleccione un Campesino de la lista";
    private ArrayList<Peasant> peasantsList;
    private EntityManagerFactory emf;

    /**
     * Creates new form PeasantsListPanel
     */
    public PeasantsListPanel(EntityManagerFactory emf) {
        this.emf = emf;
        peasantsController = new PeasantController(emf);
        validator = new InputController();
        peasantsList = new ArrayList<>();
        initComponents();
        peasantsList = peasantsController.findAllPeasants();
        initList();
        this.deleteButton.setEnabled(false);
        this.updateButton.setEnabled(false);
        this.searchButtonGroup.add(this.searchByCedulaRadioButton);
        this.searchButtonGroup.add(this.searchByNameRadioButton);
        this.searchByNameRadioButton.setSelected(true);
    }

    private void initList() {
        DefaultListModel model = new DefaultListModel();
        for (int i = 0; i < peasantsList.size(); i++) {
            model.addElement(peasantsList.get(i).getFullName());
        }
        this.peasantsJList.setModel(model);
        this.peasantsJList.revalidate();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        searchButtonGroup = new javax.swing.ButtonGroup();
        middlePanel = new javax.swing.JPanel();
        searchLabel = new javax.swing.JLabel();
        searchTextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        peasantsJList = new javax.swing.JList();
        updateButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        searchByCedulaRadioButton = new javax.swing.JRadioButton();
        searchByNameRadioButton = new javax.swing.JRadioButton();
        jPanel3 = new javax.swing.JPanel();
        logoLabel2 = new javax.swing.JLabel();
        titleLabel2 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(509, 449));

        middlePanel.setPreferredSize(new java.awt.Dimension(489, 346));

        searchLabel.setText("Buscar Campesinos:");

        searchTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                searchTextFieldKeyTyped(evt);
            }
        });

        peasantsJList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                peasantsJListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(peasantsJList);

        updateButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/update.png"))); // NOI18N
        updateButton.setText("Actualizar");
        updateButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                updateButtonMouseClicked(evt);
            }
        });

        deleteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Cancelar.png"))); // NOI18N
        deleteButton.setText("Eliminar");
        deleteButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                deleteButtonMouseClicked(evt);
            }
        });

        searchByCedulaRadioButton.setText("Cedula");

        searchByNameRadioButton.setText("Nombre");

        javax.swing.GroupLayout middlePanelLayout = new javax.swing.GroupLayout(middlePanel);
        middlePanel.setLayout(middlePanelLayout);
        middlePanelLayout.setHorizontalGroup(
            middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, middlePanelLayout.createSequentialGroup()
                .addGroup(middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(middlePanelLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(searchByNameRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchByCedulaRadioButton))
                    .addGroup(middlePanelLayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(updateButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(middlePanelLayout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addGroup(middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1)
                            .addGroup(middlePanelLayout.createSequentialGroup()
                                .addComponent(searchLabel)
                                .addGap(18, 18, 18)
                                .addComponent(searchTextField)))))
                .addGap(73, 73, 73))
        );
        middlePanelLayout.setVerticalGroup(
            middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(middlePanelLayout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchLabel)
                    .addComponent(searchTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchByCedulaRadioButton)
                    .addComponent(searchByNameRadioButton))
                .addGap(13, 13, 13)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(middlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(deleteButton)
                    .addComponent(updateButton))
                .addContainerGap())
        );

        logoLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/farmer.png"))); // NOI18N

        titleLabel2.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        titleLabel2.setText("Buscar Campesinos");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(logoLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(titleLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(titleLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(middlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(middlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void searchTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_searchTextFieldKeyTyped
        if (searchByNameRadioButton.isSelected()) {
            validator.lettersOnly(evt);
            validator.noSpecialCharacters(evt);
            peasantsList = peasantsController.findByName(this.searchTextField.getText());
        } else {
            validator.numbersOnly(evt);
            peasantsList = peasantsController.findByCedula(Integer.parseInt(this.searchTextField.getText()));
        }
        initList();
        this.deleteButton.setEnabled(false);
        this.updateButton.setEnabled(false);
    }//GEN-LAST:event_searchTextFieldKeyTyped

    private void peasantsJListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_peasantsJListMouseClicked
        this.deleteButton.setEnabled(true);
        this.updateButton.setEnabled(true);
    }//GEN-LAST:event_peasantsJListMouseClicked

    private void updateButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_updateButtonMouseClicked
        if (peasantsJList.getSelectedIndex() >= 0 && peasantsJList.getSelectedIndex() <= peasantsList.size()) {
            JFrame frame = new JFrame();
            frame.setBounds(this.getBounds());
            frame.add(new PanelPeasantRegister(emf, peasantsList.get(peasantsJList.getSelectedIndex())));
            frame.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(new JFrame(), noElementSelectedMessage, "Dialog", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_updateButtonMouseClicked

    private void deleteButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteButtonMouseClicked
        int confirmation = JOptionPane.showConfirmDialog(new JFrame(), confirmDeleteMessage, "Dialog", JOptionPane.YES_NO_OPTION);
        if (confirmation == 0) {
             int index = peasantsJList.getSelectedIndex();
             peasantsController.deletePeasant(peasantsList.get(index));peasantsList.remove(index);
        }
        initList();
    }//GEN-LAST:event_deleteButtonMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteButton;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel logoLabel2;
    private javax.swing.JPanel middlePanel;
    private javax.swing.JList peasantsJList;
    private javax.swing.ButtonGroup searchButtonGroup;
    private javax.swing.JRadioButton searchByCedulaRadioButton;
    private javax.swing.JRadioButton searchByNameRadioButton;
    private javax.swing.JLabel searchLabel;
    private javax.swing.JTextField searchTextField;
    private javax.swing.JLabel titleLabel2;
    private javax.swing.JButton updateButton;
    // End of variables declaration//GEN-END:variables
}
